package com.efast.dmom.mes.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.dmom.mes.bean.Label;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.entity.base.IModificationFlags;
import com.efast.dmom.mes.service.LabelService;

/**
 * to achieve the label to add, delete, update and query
 * 
 * @author wj
 * @date 2020/4/8
 *
 */
@Controller
@RequestMapping(value = "/label")
public class LabelController {

	@Autowired
	private LabelService labelService;

	@RequestMapping(value = "/operateByLabel")
	public @ResponseBody ResultBean operateByLabel(@RequestBody Label label) {
		ResultBean resultBean = new ResultBean(true, ' ');
		// Judge to add
		if (label.getModificationFlag() == IModificationFlags.NEW) {
			return labelService.save(label);
			// Judge to update
		} else if (label.getModificationFlag() == IModificationFlags.MODIFIED) {
			return labelService.update(label);
			// Judge to delete
		} else if (label.getModificationFlag() == IModificationFlags.DELETED) {
			return labelService.delete(label);
		}
		return resultBean;
	}

	@RequestMapping(value = "/deleteAll")
	public @ResponseBody ResultBean deleteAll(@RequestBody Label label) {
		return labelService.deleteLabelAndLabelParam(label);
	}

	// label model preview
	@RequestMapping(value = "/previewLabel")
	public @ResponseBody ResultBean previewLable(HttpServletRequest request, HttpServletResponse response,
		long labelId) {
		return labelService.previewLable(labelId, response);
	}

}
